package com.itheima.service.store.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.QuestionItemDao;
import com.itheima.domain.store.QuestionItem;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.QuestionItemService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class QuestionItemServiceImpl implements QuestionItemService {
    @Override
    public void save(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemDao mapper = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            //id使用UUID的生成策略来获取
            String id = UUID.randomUUID().toString();
            questionItem.setId(id);
            mapper.save(questionItem);
            //4.提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(QuestionItem questionItem) {
        SqlSession sqlSession = null;

        try {
            sqlSession= MapperFactory.getSqlSession();
            QuestionItemDao mapper = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            mapper.delete(questionItem);
            TransactionUtil.commit(sqlSession);

        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void update(QuestionItem questionItem) {
        SqlSession sqlSession = null;

        try {
            sqlSession= MapperFactory.getSqlSession();
            QuestionItemDao mapper = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            mapper.update(questionItem);
            TransactionUtil.commit(sqlSession);

        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public QuestionItem findById(String id) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemDao mapper = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);

            //4.提交事务
            TransactionUtil.commit(sqlSession);
            return mapper.findById(id);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }




    @Override
    public PageInfo findAll(String questionId,int page, int size) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionItemDao mapper = MapperFactory.getMapper(sqlSession, QuestionItemDao.class);
            PageHelper.startPage(page, size);
            List<QuestionItem> all = mapper.findAll(questionId);
            PageInfo info = new PageInfo(all);
            return info;
            //4.提交事务
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
